
package com.rx.mgt.controller;  

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rx.mgt.domain.AdminUser;
import com.rx.mgt.po.UsersBo;
import com.rx.mgt.service.AdminUserService;
import com.rx.mgt.service.DomainService;
import com.rx.mgt.service.SysLogService;
import com.rx.mgt.util.IpUtil;
import com.rx.mgt.util.MD5;

/** 
 * ClassName:AdminUserController 
 * Function :
 * Reason   : 
 * Date     :Jan 31, 2017 1:55:15 PM 
 * @author   luolei 
 * @since    JDK 1.6
 */
@Controller
public class AdminUserController {
    @Autowired
    AdminUserService adminUserService;
    @Autowired
    DomainService domainService;
    @Autowired
    SysLogService sysLogService;
    @RequestMapping(value="/login.htmlx")
    public String  login(HttpServletRequest request,ModelMap model, HttpServletResponse response, AdminUser adminUser){
        String captchaToken  =  (String) request.getSession().getAttribute("captchaToken");
        Object errorCount  =  request.getSession().getAttribute(adminUser.getUser_name()+"loginError");
        if(errorCount!=null&&(Integer)errorCount>5){
            model.addAttribute("message","账号被锁定,请等待30分钟后重新登录!");
        }
        System.out.println("..captchaToken.."+captchaToken);
        if(StringUtils.isNotBlank(captchaToken)&&captchaToken.equalsIgnoreCase(adminUser.getCaptchaToken())){
            adminUser.setUser_password(MD5.MD5Encode(adminUser.getUser_password()));
            AdminUser user = adminUserService.getAdminUserByPsw(adminUser);
            if(user!=null){
                request.getSession().setAttribute("user", user);
                model.addAttribute("user",user);
                return "index";
            }else{
                if(errorCount!=null&&(Integer)errorCount>5){
                    request.getSession().setAttribute(adminUser.getUser_name()+"loginError", ((Integer)errorCount)+1);
                }
                model.addAttribute("message","账号密码不正确");
                return "login";
            }
        }
        System.out.println("111111111111111");
        model.addAttribute("user",adminUser);
        model.addAttribute("message","验证码不正确");
        
        return "login";
    }
    @RequestMapping(value="/index.htmlx")
    public String index(HttpServletRequest request,ModelMap model){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        model.addAttribute("user",user);
        model.addAttribute("adminUser",user);
        return "index";
    }

    @RequestMapping(value="/quit.htmlx")
    public String  quit(HttpServletRequest request,ModelMap model, HttpServletResponse response, AdminUser adminUser){
        request.getSession().setAttribute("user", null);
       return "login";
    }
    @RequestMapping(value="/info.htmlx")
    public String  info(HttpServletRequest request,ModelMap model, HttpServletResponse response, AdminUser adminUser){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        model.addAttribute("user",user);
       return "info";
    }
    @RequestMapping(value="/info_detail.htmlx")
    public String  infoDetail(HttpServletRequest request,ModelMap model, HttpServletResponse response, AdminUser adminUser){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        model.addAttribute("adminUser",user);
       return "info_detail";
    }

    @RequestMapping(value="/admin-list.htmlx")
    public String  adminList(ModelMap model,String type){
        List list = new ArrayList();
        if("3".equals(type)){
            list = adminUserService.getAllAdminUser("2");
        }
       // List list1 =  adminUserService.getAllAdminUser("1");
       // List list3 =  adminUserService.getAllAdminUser("3");
      //  List list= new ArrayList();
       // list.addAll(list1);
      //  list.addAll(list3);
        model.addAttribute("list",list);
        return "admin-list";
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/admin-add.htmlx")
    public String  adminAdd(ModelMap model,HttpServletRequest request,AdminUser adminUser,UsersBo usersBo){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("添加管理员", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        Map map =new HashMap();
        List list = domainService.getDomainInfoExt(usersBo);
        model.addAttribute("domainList",list);
        map.put("result", "success");
        if(adminUser.getId()==null||adminUser.getId()<1){
            model.addAttribute("adminUser",adminUser);
            model.addAttribute("method","add");
            return "admin-add";
        }
        AdminUser info= adminUserService.get(adminUser.getId());
        if(info!=null){
            model.addAttribute("adminUser",info);
        }

        return "admin-add";
    }
    
    @RequestMapping(value="/domain-admin-list.htmlx")
    public String  domainAdminList(ModelMap model,String type){
        List list =  adminUserService.getAllAdminUser(type);
        model.addAttribute("list",list);
        return "domain-admin-list";
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/stopDomainAdmin.htmlx")
    @ResponseBody
    public Map  stopDomainAdmin(HttpServletRequest request,AdminUser adminUser){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("停止管理员", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        AdminUser info= adminUserService.get(adminUser.getId());
        if(info!=null){
            info.setStatus("N");
            adminUserService.saveOrUpdate(info);
        }
        System.out.println("111111111111111");
        Map map =new HashMap();
        map.put("result", "success");
        return map;
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/startDomainAdmin.htmlx")
    @ResponseBody
    public Map  startDomainAdmin(HttpServletRequest request,AdminUser adminUser){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("开启管理员", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        AdminUser info= adminUserService.get(adminUser.getId());
        if(info!=null){
            info.setStatus("Y");
            adminUserService.saveOrUpdate(info);
        }
        Map map =new HashMap();
        map.put("result", "success");
        return map;
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/domain-admin-add.htmlx")
    public String  add(ModelMap model,HttpServletRequest request,AdminUser adminUser,UsersBo usersBo){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        if("2".equals(user.getType())){
            usersBo.setDomain(user.getDomain());
        }
        List list = domainService.getDomainInfoExt(usersBo);
        model.addAttribute("domainList",list);
        Map map =new HashMap();
        map.put("result", "success");
        if(adminUser.getId()==null||adminUser.getId()<1){
            model.addAttribute("adminUser",adminUser);
            model.addAttribute("method","add");
            return "domain-admin-add";
        }
        AdminUser info= adminUserService.get(adminUser.getId());
        if(info!=null){
            model.addAttribute("adminUser",info);
        }
        System.out.println("111111111111111");
        return "domain-admin-add";
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/domainAdminSave.htmlx")
    @ResponseBody
    public Map  domainAdminSave(ModelMap model,HttpServletRequest request,AdminUser adminUser){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("添加管理员", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        Map map =new HashMap();
        map.put("result", "success");
        AdminUser info=null;
        if(adminUser.getId()!=null){
            info= adminUserService.get(adminUser.getId());
        }
        if(info!=null){
            adminUser.setCreate_time(info.getCreate_time());
            adminUserService.saveOrUpdate(adminUser);
        }else{
            adminUserService.saveOrUpdate(adminUser);
        }
        System.out.println("111111111111111");
        return map;
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/deleteAdmin.htmlx")
    @ResponseBody
    public Map  deleteAdminDomain(HttpServletRequest request,String ids){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("删除管理员", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        Map map =new HashMap();
        if(ids!=null){
            String[] id=ids.split(",");
            for(String s:id){
                if(StringUtils.isEmpty(s))
                    continue;
                adminUserService.delete(Long.valueOf(s));
            }
        }
        map.put("result", "success");
        System.out.println("111111111111111");
        return map;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/deleteAdminBatch.htmlx",method = RequestMethod.POST)
    @ResponseBody
    public Map  deleteAdminDomainBatch(HttpServletRequest request,String ids){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("删除管理员", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        Map map =new HashMap();
        if(ids!=null){
            String[] id=ids.split(",");
            for(String s:id){
                if(StringUtils.isEmpty(s))
                    continue;
                adminUserService.delete(Long.valueOf(s));
            }
        }
        map.put("result", "success");
        return map;
    }

    @RequestMapping(value="/admin-change-password.htmlx")
    public String  adminChangePassword(ModelMap model,HttpServletRequest request,AdminUser adminUser){

        AdminUser info=null;
        if(adminUser.getId()!=null){
            info= adminUserService.get(adminUser.getId());
            model.addAttribute("adminUser",info);
        }
        System.out.println("111111111111111");
        return "admin-change-password";
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/changePswAdmin.htmlx")
    @ResponseBody
    public Map  changePswAdmin(ModelMap model,HttpServletRequest request,AdminUser adminUser){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("更改管理员密码", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        Map map =new HashMap();
        AdminUser info=null;
        if(adminUser.getId()!=null){
            info= adminUserService.get(adminUser.getId());
            if(info!=null){
//                info.setUser_password(adminUser.getUser_password());
                info.setUser_password(MD5.MD5Encode(adminUser.getUser_password()));
                adminUserService.update(info);
            }
        }
        map.put("result", "success");
        System.out.println("111111111111111");
        return map;
    }

    /**
     * 根据用户名查询管理员
     */
    @RequestMapping("/queryAdminByAdminName.htmlx")
    public String queryAdminByAdminName(@RequestParam(value="name",required = false)String name, Model model){
        List list = new ArrayList();
        if(StringUtils.isNotBlank(name)){
            list = adminUserService.getAdminUserNameByName(name);
        }else{
            list = adminUserService.getAllAdminUser("2");
        }
        model.addAttribute("list",list);
        return "admin-list";
    }
}
  